<?php
class Session {

        private $time;

        public function initialize($time = 60) {
                session_start();
                $this->time = $time;
        }

        public function __destruct() {
                unset($this);
        }

        public function registerSession($newUser) {
                $_SESSION["reg_user"] = $newUser;
                $_SESSION["ses_begin"] = $this->generateNewTime();
                $_SESSION["ses_id"] = session_id();
        }

        public function isSessionRegistered() {
                if (!empty($_SESSION["reg_user"])) {
                        return true;
                } else {
                        return false;
                }
        }

        public function getSessionId() {
                return $_SESSION["ses_id"];
        }

        public function isSessionExpired() {
                if ($_SESSION["ses_begin"] < $this->timeNow()) {
                        return true;
                } else {
                        return false;
                }
        }

        public function renewSession() {
                $_SESSION["ses_begin"] = $this->generateNewTime();
        }

        private function timeNow() {
                $currentHour = date("H"); // Get current hour.
                $currentMin = date("i"); // Get current minute.
                $currentSec = date("s"); // Get current second.
                $currentMon = date("m"); // Get current month.
                $currentDay = date("d"); // Get current day.
                $currentYear = date("y"); // Get current year.

                // Return the current time stamp.
                return mktime($currentHour, $currentMin, $currentSec, $currentMon, $currentDay, $currentYear);
        }

        private function generateNewTime() {
                $currentHour = date("H"); // Get current hour.
                $currentMin = date("i"); // Get current minute.
                $currentSec = date("s"); // Get current second.
                $currentMon = date("m"); // Get current month.
                $currentDay = date("d"); // Get current day.
                $currentYear = date("y"); // Get current year.

                // Return the current time stamp.
                return mktime($currentHour, ($currentMin + $this->time), $currentSec, $currentMon, $currentDay, $currentYear);
        }

        public function destroySession() {
                session_destroy();
                $_SESSION = array();
        }
}
?>